package itcast.JDBCutils;
	/**
	 * Druid连接池的工具类
	 */
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class NewJDBCUtils {
	  //1.定义成员变量 DataSource
	 private static DataSource ds;
	 //2、使用静态代码块加载配置文件,获取DataSource因为这是调用工具类都要进行的一次
	 static {
		 try {
			 //1、加载配置文件
			 Properties pro=new Properties();
			 pro.load(NewJDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
			 //2、获取DataSource
			 ds=DruidDataSourceFactory.createDataSource(pro);
		 } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	 }
	    /**
	     * 获取连接
	     */
	    public static Connection getConnection() throws SQLException {
	        return ds.getConnection();
	    }
	    /**
	     * 释放资源，在连接里面我们称之为，归还资源，不关闭
	     */
	   public static void close(Statement stmt,Connection conn) {
		if (stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}   
	}
	   public static void close(ResultSet rs, Statement stmt,Connection conn) {
			if (rs!=null) {
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		   if (stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}   
	}
	   /**
	     * 获取连接池方法
	     */
	    public static DataSource getDataSource(){
	        return  ds;
	    }
}
